int left_in1=5;
int left_in2=4;
int right_in1=3;
int right_in2=2;

int busy = 22;

 int sui = random(400,600); 
 //int zhuan = random(400,1000); 


 int trig = 6;
 int echo = 7;
// void setup() {
//  //舒适化
//   pinMode(left_in1,OUTPUT);
//   pinMode(left_in2,OUTPUT);
//   pinMode(right_in1,OUTPUT);
//   pinMode(right_in2,OUTPUT);
// } 
const int zuo=A0;
const int you=A1;


unsigned char text1[] ={0xBA, 0xF3, 0xCD, 0xCB};//hou
  unsigned char text2[] ={0xD7, 0xF3, 0xD7, 0xAA};//zuo
  unsigned char text3[] ={0xD3, 0xD2, 0xD7, 0xAA};//you
 unsigned char text[] ={0xC7, 0xB0, 0xBD, 0xF8};//qian
unsigned char text7[] ={0xCD, 0xA3, 0xD6, 0xB9};//ting



void speak(int text_byte_len, char* text);
void setup(){
  pinMode(left_in1,OUTPUT);
  pinMode(left_in2,OUTPUT);
  pinMode(right_in1,OUTPUT);
  pinMode(right_in2,OUTPUT);

  pinMode (busy ,INPUT);
  Serial2.begin(9600);
 

   pinMode(zuo,INPUT);  
    pinMode(you,INPUT);  


  pinMode(trig,OUTPUT);
  pinMode(echo,INPUT);
   Serial.begin(9600);   
    speak(4, text);
}







void go_hou(int qun ,int zhuan){           //hou
  analogWrite(left_in1,qun);
  digitalWrite(left_in2,LOW);
  analogWrite(right_in2,qun);
  digitalWrite(right_in1,LOW);
 delay(zhuan);
}


void go_qian(int qun ) {        //qian
 
  digitalWrite(left_in1, LOW);    // 左电机引脚1低电平
  analogWrite(left_in2, qun);   // 左电机引脚2高电平
  
  digitalWrite(right_in2, LOW);   // 右电机引脚1低电平
  analogWrite(right_in1, qun);  // 右电机引脚2高电平
}

void go_qian2(int qun, int qun2) {
  // 左电机：IN1=低电平，IN2=PWM（正转）
  digitalWrite(left_in1, LOW);
  analogWrite(left_in2, qun);

  // 右电机：IN1=PWM，IN2=低电平（正转）
  analogWrite(right_in1, qun2);
  digitalWrite(right_in2, LOW);
}

void go_qian3(int qun) {        //qian
 
  digitalWrite(left_in1, LOW);    // 左电机引脚1低电平
  analogWrite(left_in2, qun);   // 左电机引脚2高电平
  
  digitalWrite(right_in2, LOW);   // 右电机引脚1低电平
  analogWrite(right_in1, qun);  // 右电机引脚2高电平
}



void stop(){              //停止
  digitalWrite(left_in2,LOW);
  digitalWrite(left_in1,LOW);
  digitalWrite(right_in1,LOW);
  digitalWrite(right_in2,LOW);
}

void go_zuo(int qun,int zhuan){      //左
analogWrite(left_in2,qun);
digitalWrite(left_in1,LOW);
analogWrite(right_in2,qun);
digitalWrite(right_in1,LOW);
 delay(zhuan);
}
void go_zuo2(int qun,int you){      //左
analogWrite(left_in2,qun);
digitalWrite(left_in1,LOW);
analogWrite(right_in2,you);
digitalWrite(right_in1,LOW);
//  delay(zhuan);
}
void go_you2(int zuo,int you){     //右
analogWrite(left_in1,zuo);
digitalWrite(left_in2,LOW);
analogWrite(right_in1,you);
digitalWrite(right_in2,LOW);

}

void go_zuo1(int qun){      //左
analogWrite(left_in2,qun);
digitalWrite(left_in1,LOW);
analogWrite(right_in2,qun);
digitalWrite(right_in1,LOW);
 delay(sui);
}
void go_you1(int qun){     //右
analogWrite(left_in1,qun);
digitalWrite(left_in2,LOW);
analogWrite(right_in1,qun);
digitalWrite(right_in2,LOW);
 delay(sui);
}

// void birang() {
//     zhuan += 500;  // 直接增加
//     if (zhuan > 1000) {  // 限制最大值
//         zhuan = 0;
//     }

// void birang() {
  

// if ((digitalRead(qian) == HIGH) && (digitalRead(you) == HIGH)) {
  
    
//     // do {go_qian2(120,100);} 
//     while((digitalRead(qian) == HIGH) && (digitalRead(you) == HIGH));
//     go_qian(0);
//     delay(1000);
// } 
// else if ((digitalRead(qian) == LOW) && (digitalRead(you) == HIGH)) {
//     // speak(4, text1);   
//     // go_hou(100);    delay(100);
//     speak(4, text3); 
       
//     do go_zuo(70,);
//     // zhuan += 500;
//   //  if (zhuan > 1000) { 
//     // zhuan = 400;
//   // }

//      while((digitalRead(qian) == LOW) && (digitalRead(you) == HIGH));

//   go_qian(0);
//   delay(1000);
  
// } 
// else if ((digitalRead(qian) == HIGH) && (digitalRead(you) == LOW)) {
//  speak(4, text2);  
//    do
//     go_you(70);
//       // zhuan += 500; 
//   //  if (zhuan > 1000) { 
//     // zhuan = 400;
  
//   // }

  
//     while((digitalRead(qian) == HIGH) && (digitalRead(you) == LOW));

//   go_qian(0);
//   delay(1000);
   
// }
//  else {
//   stop();
//   go_you1(100); 
//   // stop();
//   go_you1(100); 
//   // stop();
//   go_zuo1(100);
//   //  stop();
 
//    go_hou(50);
 
//  }

// }




//语音二
void speak_string(String text){
char frameHead[] = {0xfd,0x00,0x00,0x01,0x01}; 
int length = text.length();
int i = 0;
int eor = 0;


frameHead[2]=length+3;
if(!digitalRead(busy))
{  
for(i=0;i<5;i++)
{
Serial2.write(frameHead[i]);
              eor = eor^frameHead[i];
}
for(i=0;i<length;i++)
{
Serial2.write(text[i]);
                eor = eor^text[i];
}
Serial2.write(eor);
}
while(digitalRead(busy));
}

//语音输出初始化
// void speak(int text_byte_len, char* text) {  // 参数改为字节长度
//   unsigned char frame_head = 0xFD;
//   unsigned char frame_length[] = {0x00, text_byte_len + 3}; // 直接使用字节长度
//   unsigned char frame_com = 0x01;
//   unsigned char frame_pra = 0x00;
//   unsigned char frame_check = 0;

//   // 忙检测（带超时）
//   unsigned long startTime = millis();
//   while (digitalRead(busy) == HIGH) {
//     if (millis() - startTime > 5000) {
//       Serial.println("Timeout!");
//       return;
//     }
//     delay(200);
//   }

//   // 发送帧
//   Serial2.write(frame_head);
//   frame_check ^= frame_head;
//   for (int i = 0; i < 2; i++) {
//     Serial2.write(frame_length[i]);
//     frame_check ^= frame_length[i];
//   }
//   Serial2.write(frame_com);
//   frame_check ^= frame_com;
//   Serial2.write(frame_pra);
//   frame_check ^= frame_pra;
//   for (int i = 0; i < text_byte_len; i++) { // 按字节长度发送
//     Serial2.write(text[i]);
//     frame_check ^= text[i];
//   }
//   Serial2.write(frame_check);
//   delay(500);
// }

//语音输出
void speak(int text_len, char* text){
  unsigned char frame_head =0xFD;
  unsigned char frame_length[] = {0x00,text_len + 3};
  unsigned char frame_com  = 0x01;
  unsigned char frame_pra = 0x00;
 
  unsigned char frame_check=0;

  int i = 0;

  while(digitalRead(busy)==HIGH) delay(200);


  //发送
  Serial2.write(frame_head);
  //校验
  frame_check ^= frame_head;
  for(i=0; i<2; i++){Serial2.write(frame_length[i]); frame_check ^=frame_length[i];}

  Serial2.write(frame_com);frame_check ^=frame_com;  //发送并校验
  Serial2.write(frame_pra);frame_check ^=frame_pra; 
  for(i=0;i< text_len ; i++){Serial2.write(text[i]); frame_check ^=text[i];}

  Serial2.write(frame_check);
  
   delay(500);
}

 float distance;
void muce(){

 
digitalWrite(trig,LOW);
delayMicroseconds(2);
digitalWrite(trig,HIGH);
delayMicroseconds(10);
digitalWrite(trig,LOW);
delayMicroseconds(2);
distance= pulseIn(echo,HIGH)*0.017;

// Serial.println(distance);
delay(500);

}




void zuihou(){

digitalWrite(trig,LOW);
delayMicroseconds(2);
digitalWrite(trig,HIGH);
delayMicroseconds(10);
digitalWrite(trig,LOW);
delayMicroseconds(2);
distance= pulseIn(echo,HIGH)*0.017;
delay(10);

// int speed = map(distance, min_distance, max_distance, min_speed, max_speed);
// speed = constrain(speed, min_speed, max_speed);
int min_speed = 60;   // 最小速度（距离最近时）
int max_speed = 100;  // 最大速度（距离最远时）
int min_distance = 0; // 最近有效距离（厘米）
int max_distance = 100;// 最远有效距离（厘米）
// 指数衰减：距离越近，速度下降越快
float ratio = pow((float)(distance - min_distance) / (max_distance - min_distance), 2);
int speed = min_speed + (max_speed - min_speed) * ratio;
speed = max(min_speed, speed); // 确保不低于最小值



//speak_string(String(distance));  
          // 播报当前距离

// if((digitalRead(zuo) == HIGH) && (digitalRead(you) == HIGH)   && distance >=15){


// go_qian2(speed+20,speed);  

// // go_zuo(100,1000);
// // go_you(100,2000);

// }
//  if((digitalRead(zuo) == HIGH) && (digitalRead(you) == HIGH)   && distance <15) {

 
// go_hou(100,500);
//  delay(100);
// go_zuo(90,1000);
  
//   if((digitalRead(zuo) == LOW) && (digitalRead(you) == HIGH) && distance <15){
// go_you(90,2000);
//   }
// }
// if((digitalRead(zuo) == LOW) && (digitalRead(you) == LOW)   && distance <15) {

// go_hou(100,500);
//  delay(100);
// go_zuo(90,1000);
  
//   if((digitalRead(zuo) == LOW) && (digitalRead(you) == HIGH) && distance <15){
// go_you(90,2000);
//   }
// }

//  if ((digitalRead(zuo) == LOW) && (digitalRead(you) == HIGH) && distance >50) {   
//       speak(4, text3); 
      
//     do 
//       go_zuo2(70,120,200);
 

//      while((digitalRead(zuo) == LOW) && (digitalRead(you) == HIGH) &&  distance >50);

  

  
// } 
//  if ((digitalRead(zuo) == LOW) && (digitalRead(you) == HIGH) && distance <50) {   
//       speak(4, text3); 
//       go_hou(200,100);
//     do 
    
//       go_zuo2(70,120,800);
  

//      while((digitalRead(zuo) == LOW) && (digitalRead(you) == HIGH) &&  distance <50);


  
// } 
//  if ((digitalRead(zuo) == HIGH) && (digitalRead(you) == LOW) &&  distance >50) {
//     speak(4, text2); 
      
//   do
//     go_you(70,200);

//   while((digitalRead(zuo) == HIGH) && (digitalRead(you) == LOW) &&  distance >50);
  
// }
//  if ((digitalRead(zuo) == HIGH) && (digitalRead(you) == LOW) &&  distance <50) {
//     speak(4, text2);  
//     go_hou(100,200);
//     delay(100);
//   do
//     go_you(70,500);


//   while((digitalRead(zuo) == HIGH) && (digitalRead(you) == LOW) &&  distance <50);
  
// }
int p=0;
// }
int x=0;
if((digitalRead(zuo) == HIGH) && (digitalRead(you) == HIGH)   && distance >=15 && x==0){
  
   go_qian2(speed+20,speed);  
}
if((digitalRead(zuo) == HIGH) && (digitalRead(you) == HIGH)   && distance >=15 && x==1){

   go_qian2(speed+80,speed);  
}
//小幅度
if((digitalRead(zuo) == LOW) && (digitalRead(you) == HIGH)  ){
// go_you(90,500);
go_zuo2(60,60);
delay(500);
}
if((digitalRead(zuo) == HIGH) && (digitalRead(you) == LOW)  ){
// go_zuo2(70,120,500);
x=1;
go_you2(90,90);
delay(500);

}


if((digitalRead(zuo) == LOW) && (digitalRead(you) == LOW)   && distance <=25){
 go_hou(60,100);
 delay(100);
go_zuo2(60,60);
delay(100);
  
  if((digitalRead(zuo) == LOW) && (digitalRead(you) == LOW) && distance <25){
go_you2(60,60);
delay(200);
  }
}

}




void loop() {

// muce();
 zuihou();
// go_zuo(80,1000);


  
 
//   unsigned char text4[] ={0xB5,0xB9,0xB3,0xB5,0xC7,0xEB,0xD7,0xA2,0xD2,0xE2};//倒车请注意
//   unsigned char text5[] = {
//     0xCF, 0xE9,  // 祥
//     0xD7, 0xD3,  // 子
//     0xBA, 0xC5,  // 号
//     0xC0, 0xB4,  // 来
//     0xC1, 0xCB   // 了
// };
// unsigned char text6[] = {
//     0xC0, 0xB4,  // 来
//     0xB2, 0xC6   // 财
// };
  


 
// if ((digitalRead(qian) == HIGH) && (digitalRead(you) == HIGH)) {
  
//     speak(4, text);    
//     do {go_qian2(120,100);} while((digitalRead(qian) == HIGH) && (digitalRead(you) == HIGH));
//     go_qian(0);
//     delay(1000);
// } 
// else if ((digitalRead(qian) == LOW) && (digitalRead(you) == HIGH)) {
//     // speak(4, text1);   
//     // go_hou(100);    delay(100);
//     speak(4, text3); 
       
//     do go_zuo(100);
   

//      while((digitalRead(qian) == LOW) && (digitalRead(you) == HIGH));

//   go_qian(0);
//   delay(1000);
  
// } 
// else if ((digitalRead(qian) == HIGH) && (digitalRead(you) == LOW)) {
//  speak(4, text2);  
//    do
//     go_you(100);
  
//     while((digitalRead(qian) == HIGH) && (digitalRead(you) == LOW));

//   go_qian(0);
//   delay(1000);
   
// }
//  else {
//   stop();
 
//  go_zuo1(100);
    
//  go_you1(100); 
 
   
//  }

/*unsigned char text7[] = {
    0xC4, 0xE3,  // 你
    0xD2, 0xB2,  // 也
    0xCF, 0xEB,  // 想
    0xB8, 0xF8,  // 跟
    0xCE, 0xD2,  // 我
    0xB5, 0xC4,  // 的
    0x41, 0x45,  // A E (ASCII码)
    0xB0, 0xCB, 
    0xC1, 0xF9, // 8 6 (ASCII码)
    0xB1, 0xC8,  // 比
    0xD2, 0xBB,  // 一
    0xB1, 0xC8,  // 比
    0xC2, 0xF0    // 吗
};

*/
//   speak(sizeof(text5), (char*)text5);
//  delay(2000);

//  speak(sizeof(text6), (char*)text6);

 // speak(26,text7);
  //delay(1000);




  // speak(4,text);
  
  // go_qian(100);//最大255
  // delay(1000);

  // stop();
  // delay(1000);


//   speak(4,text3);
//   go_you(50);
//   delay(650);
 

//  stop();
//   delay(1000);

 
//   speak(4,text);
//   go_qian(100);//最大255
//   delay(2000);

//   stop();
//   delay(1000);


//   speak(4,text2);
//   go_zuo(50);
//   delay(690);

//  stop();
//   delay(1000);

 
//   speak(4,text);
//   go_qian(100);//最大255
//   delay(2000);

//   stop();
//   delay(1000);


//   speak(4,text2);
//   go_zuo(50);
//   delay(690);

//   stop();
//   delay(1000);

 
//   speak(4,text);
//   go_qian(100);//最大255
//   delay(2000);

//   stop();
//   delay(1000);


  // speak(4,text3);
//   go_you(50);
  // delay(650);
 
//   stop();
//   delay(1000);

// speak(4,text1);
//  go_hou(100);
// delay(1000);
  //  stop();
  // delay(1000);



}
